<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>信息编辑</title>

    <link rel="stylesheet" href="./assets/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="./assets/css/animate.min.css"/>
    <link rel="stylesheet" href="./assets/bootstrap-validator/bootstrapValidator.min.css">
    <link rel="stylesheet" href="./assets/mui/css/mui.min.css">
    <link rel="stylesheet" href="./assets/city-picker/city-picker.css">

    <link rel="stylesheet/less" href="./assets/css/reset.less"/>
    <link rel="stylesheet/less" href="./assets/css/common.less"/>
    <link rel="stylesheet/less" href="./assets/css/ContactAdd.less"/>
</head>
<body>

<div class="info">
    <h3 class="title wow fadeInUp">信息编辑</h3>

    <p class="desc wow fadeInUp" data-wow-delay="200ms">
        您可在本页修改您的联系信息，修改成功后名片将会出现在首页。
    </p>
</div>

<form class="contact wow fadeInUp" data-wow-delay="300ms" id="edit">
    <div class="form-group">
        <label for="nickname">姓名</label>
        <input type="text" class="form-control" id="nickname" placeholder="请输入姓名" required/>
    </div>

    <div class="form-group">
        <label for="mobile">手机</label>
        <input type="text" class="form-control" id="mobile" placeholder="请输入手机" required/>
    </div>

    <div class="form-group">
        <label for="gender">性别</label>
        <select class="form-control" id="gender" name="gender">
            <option value="0">保密</option>
            <option value="1">男</option>
            <option value="2">女</option>
        </select>
    </div>

    <div class="form-group" style="position: relative;">
        <label for="region">地区</label>
        <input id="region" class="form-control" data-toggle="city-picker" type="text" value=""/>
    </div>

    <div class="form-group">
        <label for="typeId">分类</label>
        <select class="form-control" id="typeId" name="typeId">
            <option>请选择</option>
        </select>
    </div>

    <div class="form-group">
        <label for="remark">详细地址</label>
        <input type="text" class="form-control" id="remark" placeholder="请输入详细地址"/>
    </div>

    <div class="action">
        <button class="btn">提交</button>
    </div>
</form>
</body>
</html>

<script src="./assets/js/less.min.js"></script>
<script src="./assets/js/jquery.min.js"></script>
<script src="./assets/js/bootstrap.min.js"></script>
<script src="./assets/js/wow.min.js"></script>

<script src="./assets/bootstrap-validator/bootstrapValidator.min.js"></script>
<script src="./assets/mui/js/mui.min.js"></script>
<script src="./assets/city-picker/city-picker.data.min.js"></script>
<script src="./assets/city-picker/city-picker.min.js"></script>
<script src="./assets/axios/axios.min.js"></script>
<script src="./assets/axios/request.js"></script>
<script src="./assets/js/cookie.js"></script>
<script src="./assets/js/login.js"></script>
<script src="./assets/js/helpers.js"></script>

<script>
    new WOW().init()

    let cardid = getParams('cardid');

    let business = cookie.get('business') ? JSON.parse(cookie.get('business')) : {};

    // 请求通讯录详情或者验证该联系人是否存在
    (async () => {
        let data = {
            cardid,
            businessid: business.id
        }

        let result = await POST({
            url: 'card/card/check',
            params: data
        });

        if (result) {
            if (result.code === 0) {
                mui.toast(result.msg)

                setTimeout(() => {
                    history.go(-1)
                }, 2000)

                return
            }

            let data = result.data

            $('#nickname').val(data.nickname)
            $('#mobile').val(data.mobile)
            $('#gender').val(data.gender)

            $('#region').citypicker('reset');
            $('#region').citypicker('destroy');

            $('#region').citypicker({
                province: data.province_text,
                city: data.city_text,
                district: data.district_text
            });

            $('#typeId').val(data.typeid)
            $('#remark').val(data.remark)
        }
    })();

    // 表单提交事件
    $('#edit').on('submit', async () => {
        if (!$("#edit").data('bootstrapValidator').isValid()) {
            $('#edit').data('bootstrapValidator').resetForm()
            return
        }

        let typeid = $.trim($('#typeId').val());

        if (typeid === '请选择') {
            mui.toast('请选择通讯录分类')
            return
        }

        let data = {
            cardid,
            businessid: business.id,
            nickname: $.trim($('#nickname').val()),
            mobile: $.trim($('#mobile').val()),
            gender: $.trim($('#gender').val()),
            typeid: typeid,
            remark: $.trim($('#remark').val())
        }

        // 获取相应地区码
        let citypicker = $('#region').data("citypicker")
        let code = citypicker.getCode('district') || citypicker.getCode('city') || citypicker.getCode('province')

        if (code) {
            data.region = code
        }

        let result = await POST({
            url: 'card/card/edit',
            params: data
        });

        if (result) {
            if (result.code === 1) {
                mui.toast(result.msg)

                setTimeout(() => {
                    location.href = 'index.html'
                }, 2000)
            } else {
                mui.toast(result.msg)
            }
        }
    }).bootstrapValidator({
        message: '表单内容不能为空',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            mobile: {
                message: '手机号码验证失败',
                validators: {
                    notEmpty: {
                        message: '手机号码不能为空'
                    },
                    stringLength: {
                        min: 11,
                        max: 11,
                        message: '手机号码长度为11位'
                    },
                    regexp: {
                        regexp: /^1[3-9][0-9]{9}$/,
                        message: '手机号码格式有误'
                    }
                }
            },
            remark: {
                validators: {
                    notEmpty: {
                        message: '请输入详细地址'
                    },
                }
            },
            nickname: {
                validators: {
                    notEmpty: {
                        message: '请输入姓名'
                    }
                }
            },
        }
    }).on('success.form.bv', function (e) {
        e.preventDefault()
    });

    (async () => {
        let result = await POST({
            url: 'card/card/type',
        });

        if (result) {
            if (result.code === 0) {
                $('#typeId').append(`<option>暂无分类</option>`);

                return;
            }

            let option = '';

            for (let item of result.data) {
                option += `<option value="${item.id}">${item.name}</option>`
            }

            $('#typeId').append(option);
        }
    })();
</script>